package com.example.main.login

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.lib_base.mvvm.BaseViewActivity
import com.example.main.R
import com.example.main.databinding.ActivityRegisterBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/login/RegisterActivity")
@AndroidEntryPoint
class RegisterActivity : BaseActivity<ActivityRegisterBinding,LoginViewModel>() {

    private var generatedCode: String? = null
    private var countDownTimer: android.os.CountDownTimer? = null
    private var isCountingDown = false
    override fun initData() {
        vm.RegisterSuccess.observe(this){
            android.widget.Toast.makeText(this, "注册成功，返回登录", android.widget.Toast.LENGTH_SHORT).show()
            finish()
        }

    }

    override fun initView() {
        // 获取验证码
        binding.btnGetCode.setOnClickListener {
            if (isCountingDown) {
                android.widget.Toast.makeText(this, "倒计时结束后方可再次点击", android.widget.Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            generatedCode = (100000..999999).random().toString()
            android.widget.Toast.makeText(this, generatedCode, android.widget.Toast.LENGTH_SHORT).show()
            // 启动倒计时
            isCountingDown = true
            binding.btnGetCode.isEnabled = false
            countDownTimer = object : android.os.CountDownTimer(60000, 1000) {
                override fun onTick(millisUntilFinished: Long) {
                    binding.btnGetCode.text = "${millisUntilFinished / 1000}s"
                }
                override fun onFinish() {
                    isCountingDown = false
                    binding.btnGetCode.isEnabled = true
                    binding.btnGetCode.text = "获取验证码"
                }
            }.start()
        }

        binding.btnRegister.setOnClickListener {
            val inputCode = binding.etCode.text.toString()
            val newPw = binding.etPassword.text.toString()
            val confirmPw = binding.etConfirmPassword.text.toString()
            val phone = binding.etPhone.text.toString().trim() // 获取输入并去除空格

// 国际手机号正则（支持带 + 号和空格）
            //val pattern = "^\\+(?:[0-9] ?){6,14}[0-9]$"

// 中国大陆手机号正则（11位，1开头）
            val pattern = "^1[3-9]\\d{9}$"

            if (!phone.matches(pattern.toRegex())) {
                // 手机号格式正确
                ToastUtils.showLong("手机号格式错误")
                return@setOnClickListener
            }
            if (inputCode != generatedCode) {
                android.widget.Toast.makeText(this, "验证码错误", android.widget.Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (newPw != confirmPw) {
                android.widget.Toast.makeText(this, "两次密码不一致", android.widget.Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            vm.registerList(binding.etPhone.text.toString(), newPw)
            // 验证通过，返回登录页面

        }
        // 返回登录
        binding.tvLogin.setOnClickListener {
            finish()
        }

    }
    override fun onDestroy() {
        super.onDestroy()
        countDownTimer?.cancel()
    }

}